/* "Efficiency and water use: Dynamic effects of irrigation technology adoption"
by Micah Cameron-Harp and Nathan Hendricks

Code written by Micah Cameron-Harp
May 16th, 2024

This do file reads the TWFE, Callaway & Sant'Anna ATT, and 
	de Chaisemartin & D'Haultfoueille (2021) estimates,
	saves them (used to create Table D.10), and generates figure D.3.
*/

/* NOTE - Global macros for directories are created in the appendices_results.do 
	file which calls this individual .do file */

********************************************************************************
/* Start with Flood to CP or LEPA */
********************************************************************************
*Load the twfe estimates, store the mean of the dependent variables
import excel "${dr_temp}\twfe.xlsx", firstrow sheet(floodtocplepa_91_15_nyt) clear	
		sum panel_mean_dep_var if dep_var=="af_used"
		local mean_af_used = r(mean)
		sum panel_mean_dep_var if dep_var=="acres_irr"
		local mean_acres_irr = r(mean)
		sum panel_mean_dep_var if dep_var=="depth_applied"
		local mean_depth_applied = r(mean)
*Now load CD estimates
use "${dr_temp}\didl_flood_cplepa_af_used.dta", clear
	gen dep_var = "af_used"
	gen panel_mean_dep_var=`mean_af_used'
	*Append the other two dependent variables 
	append using "${dr_temp}\didl_flood_cplepa_acres_irr.dta", force
	replace dep_var = "acres_irr" if missing(dep_var)
	replace panel_mean_dep_var=`mean_acres_irr' if missing(panel_mean_dep_var)
	append using "${dr_temp}\didl_flood_cplepa_depth_applied.dta", force
	replace dep_var = "depth_applied" if missing(dep_var)
	replace panel_mean_dep_var=`mean_depth_applied' if missing(panel_mean_dep_var)
	keep if time_to_treatment==.
	gen est = "cd"
	rename (treatment_effect se_treatment_effect treatment_effect_upper_95CI treatment_effect_lower_95CI) ///
		(estimate se_estimate ub_estimate lb_estimate)
	gen scaled_estimate = 100*estimate/panel_mean_dep_var
	gen scaled_lb = 100*lb_estimate/panel_mean_dep_var
	gen scaled_ub = 100*ub_estimate/panel_mean_dep_var
	keep dep_var est scaled_estimate scaled_lb scaled_ub estimate se_estimate lb_estimate ub_estimate

	*Append the TWFE and CS results 
	append using "${dr_temp}\floodtocplepa_twfe.dta"
	append using "${dr_temp}\floodtocplepa_cs.dta"

******* Table D.10 - ATT estimates in levels for TWFE, CS, and CD estimators *********
	*Generate column indicating tech transition and save
	gen transition = "flood_to_cp"
	save "${dr_temp}\floodtocplepa_att_3est.dta", replace 

********************************************************************************
/* Now CP to LEPA */
********************************************************************************	
*Load the twfe estimates, store the mean of the dependent variables
import excel "${dr_temp}\twfe.xlsx", firstrow sheet(cptolepa_91_19) clear
		sum panel_mean_dep_var if dep_var=="af_used"
		local mean_af_used = r(mean)
		sum panel_mean_dep_var if dep_var=="acres_irr"
		local mean_acres_irr = r(mean)
		sum panel_mean_dep_var if dep_var=="depth_applied"
		local mean_depth_applied = r(mean)
*Now load CD estimates
use "${dr_temp}\didl_cp_lepa_af_used.dta", clear
	gen dep_var = "af_used"
	gen panel_mean_dep_var=`mean_af_used'
	*Append the other two dependent variables 
	append using "${dr_temp}\didl_cp_lepa_acres_irr.dta", force
	replace dep_var = "acres_irr" if missing(dep_var)
	replace panel_mean_dep_var=`mean_acres_irr' if missing(panel_mean_dep_var)
	append using "${dr_temp}\didl_cp_lepa_depth_applied.dta", force
	replace dep_var = "depth_applied" if missing(dep_var)
	replace panel_mean_dep_var=`mean_depth_applied' if missing(panel_mean_dep_var)
	keep if time_to_treatment==.
	gen est = "cd"
	rename (treatment_effect se_treatment_effect treatment_effect_upper_95CI treatment_effect_lower_95CI) ///
		(estimate se_estimate ub_estimate lb_estimate)
	gen scaled_estimate = 100*estimate/panel_mean_dep_var
	gen scaled_lb = 100*lb_estimate/panel_mean_dep_var
	gen scaled_ub = 100*ub_estimate/panel_mean_dep_var
	keep dep_var est scaled_estimate scaled_lb scaled_ub estimate se_estimate lb_estimate ub_estimate
	
	*Append the TWFE and CS results 
	append using "${dr_temp}\cptolepa_twfe.dta"
	append using "${dr_temp}\cptolepa_cs.dta"

******* Table D.10 - ATT estimates in levels for TWFE, CS, and CD estimators *********
	*Generate column indicating tech transition and save
	gen transition = "cp_to_lepa"
	*Append the data for flood to cp transition
	append using "${dr_temp}\floodtocplepa_att_3est.dta"
	*Save
	export delimited using "${dr_output_app}/tableD10.csv", replace	
	
******************** Figure D.3 ****************************************
	*Make numerical variable encoding estimator 
	gen estimator = 2 if est=="cd"
	replace estimator = 3 if est=="twfe"
	replace estimator = 1 if est=="cs"

	*Acre-ft,
	tw (rcap scaled_lb scaled_ub estimator if transition=="flood_to_cp" & ///
		dep_var=="af_used", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="flood_to_cp" & ///
		dep_var=="af_used", mcolor("navy") msize(small)), ///  
		title("Acre-feet withdrawn", size(mediumsmall)) ///
		yscale(range(-15 15)) ylabel(-15(5)15, labsize(small)) xscale(range(.8 3.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 3 "{it}{&beta}{superscript:TWFE}" 2 "{it}{&delta}{superscript:CD}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		text(23 .3 "A. Flood to center-pivot", place(e) size(medium)) ///
		xsize(2.5) ysize(3) ///
		name(flood_cp_acreft, replace)		
		
		// 		legend(order(2) lab(2 "Estimate as percent of the dependent variable's mean value")) ///
		
	*Acres irrigated,
	tw (rcap scaled_lb scaled_ub estimator if transition=="flood_to_cp" & ///
		dep_var=="acres_irr", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="flood_to_cp" & ///
		dep_var=="acres_irr", mcolor("navy") msize(small)), ///  
		title("Acres irrigated", size(mediumsmall)) ///
		yscale(range(-15 15)) ylabel(-15(5)15, labsize(small)) xscale(range(.8 3.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 3 "{it}{&beta}{superscript:TWFE}" 2 "{it}{&delta}{superscript:CD}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		xsize(2) ysize(3) ///
		name(flood_cp_acresirr, replace)
		
	*Depth applied,
	tw (rcap scaled_lb scaled_ub estimator if transition=="flood_to_cp" & ///
		dep_var=="depth_applied", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="flood_to_cp" & ///
		dep_var=="depth_applied", mcolor("navy") msize(small)), ///  
		title("Depth applied", size(mediumsmall)) ///
		yscale(range(-15 15)) ylabel(-15(5)15, labsize(small)) xscale(range(.8 3.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 3 "{it}{&beta}{superscript:TWFE}" 2 "{it}{&delta}{superscript:CD}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		xsize(2) ysize(3) ///
		name(flood_cp_depthapp, replace)
		
	*Combine all the flood to cp graphs 
		graph combine flood_cp_acreft flood_cp_acresirr flood_cp_depthapp, ///
		rows(1) cols(3) ///
		l1title("Percent change", size(small)) ///
		xsize(6.5) ysize(2.65) name(flood_cp_all, replace)
	
**# CP to cp with Lepa
	*Acre-ft,
	tw (rcap scaled_lb scaled_ub estimator if transition=="cp_to_lepa" & ///
		dep_var=="af_used", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="cp_to_lepa" & ///
		dep_var=="af_used", mcolor("navy") msize(small)), ///  
		title("Acre-feet withdrawn", size(mediumsmall)) ///
		yscale(range(-10 5)) ylabel(-10(2.5)5, labsize(small)) xscale(range(.8 3.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 3 "{it}{&beta}{superscript:TWFE}" 2 "{it}{&delta}{superscript:CD}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		text(8.8 .29 "B. Adopting LEPA", place(e) size(medium)) ///
		xsize(2.5) ysize(3) ///
		name(cp_lepa_acreft, replace) 
		
	*Acres irrigated,
	tw (rcap scaled_lb scaled_ub estimator if transition=="cp_to_lepa" & ///
		dep_var=="acres_irr", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="cp_to_lepa" & ///
		dep_var=="acres_irr", mcolor("navy") msize(small)), ///  
		title("Acres irrigated", size(mediumsmall)) ///
		yscale(range(-10 5)) ylabel(-10(2.5)5, labsize(small)) xscale(range(.8 3.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 3 "{it}{&beta}{superscript:TWFE}" 2 "{it}{&delta}{superscript:CD}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) /// 
		xsize(2) ysize(3) ///
		name(cp_lepa_acresirr, replace)
		
	*Depth applied,
	tw (rcap scaled_lb scaled_ub estimator if transition=="cp_to_lepa" & ///
		dep_var=="depth_applied", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="cp_to_lepa" & ///
		dep_var=="depth_applied", mcolor("navy") msize(small)), ///  
		title("Depth applied", size(mediumsmall)) ///
		yscale(range(-10 5)) ylabel(-10(2.5)5, labsize(small)) xscale(range(.8 3.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 3 "{it}{&beta}{superscript:TWFE}" 2 "{it}{&delta}{superscript:CD}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		xsize(2) ysize(3) ///
		name(cp_lepa_depthapp, replace)
		
	*Combine all the cp to LEPA graphs
		graph combine cp_lepa_acreft cp_lepa_acresirr cp_lepa_depthapp, ///
		rows(1) cols(3) ///
		l1title("Percent change", size(small)) ///
		xsize(6.5) ysize(2.65) name(cp_lepa_all, replace)
	
*Combine the graphs 	
graph combine flood_cp_all cp_lepa_all, ///
	rows(2) cols(1) ///
	b1title("Estimator", size(small)) ///
	xsize(6.5) ysize(8) 
graph export "${dr_output_app}/figureD3.tif", width(6500) height(7900) replace 

